var gulp = require('gulp');
var rename = require('gulp-rename');
var path = require('path');
var template = require('gulp-template');
var browserSync = require('browser-sync');
/***Less refer**/
var less = require('gulp-less');
var LessAutoprefix = require('less-plugin-autoprefix');
var autoprefix = new LessAutoprefix({ browsers: ['last 2 versions'] });
var LessPluginCleanCSS = require('less-plugin-clean-css'),
    cleanCSSPlugin = new LessPluginCleanCSS({advanced: true});
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var concatCss = require('gulp-concat-css');
var cleanCSS = require('gulp-clean-css');
var htmlreplace = require('gulp-html-replace');

// var rev = require('gulp-rev'); //- 对文件名加MD5后缀

// var revCollector = require('gulp-rev-collector');

/** image **/
// var imagemin = require('gulp-imagemin');
var yargs = require('yargs').argv;
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');


gulp.task('build:images', function(){
    gulp.src('src/images/*')
    // .pipe(imagemin())
    .pipe(gulp.dest('production/images'))  
});

gulp.task('fonts', function(){
  gulp.src('src/fonts/*')
  .pipe(gulp.dest('production/fonts'));
});

gulp.task('build:javascripts', function(){
    gulp.src(['src/js/config.js', 'src/js/app-controller.js', 'src/js/constant.js'])
        .pipe(uglify())
        .pipe(gulp.dest('production/js'))

    gulp.src(['src/js/services.js', 'src/js/service/**/*.js'])
        .pipe(concat('services.js'))
        .pipe(uglify())
        .pipe(gulp.dest('production/js'))

    gulp.src(['src/js/filters.js', 'src/js/filter/**/*.js'])
        .pipe(concat('filters.js'))
        .pipe(uglify())
        .pipe(gulp.dest('production/js'))

    gulp.src(['src/js/directives.js', 'src/js/directive/**/*.js'])
        .pipe(concat('directives.js'))
        .pipe(uglify())
        .pipe(gulp.dest('production/js'))
    gulp.src(['src/page/**/*.js','src/component/**/*.js'])
        .pipe(concat('controllers.js'))
        .pipe(uglify())
        .pipe(gulp.dest('production/js'))
});

gulp.task('build:page:html', function () {
  gulp.src( 'src/component/**/*.html')
      .pipe(gulp.dest('production/component'));
  gulp.src('src/page/**/*.html')
      .pipe(gulp.dest('production/page'))
});


gulp.task('build:css', function() {
  return gulp.src('src/style/css/**.css')
    .pipe( concatCss("bundle.min.css") )
    .pipe( cleanCSS() )
    .pipe(gulp.dest('production/style'))
});

gulp.task('build:less', ['build:css'],  function () {
  return gulp.src('src/style/less/index.less')
    .pipe(less({
      plugins: [autoprefix],
        paths: [ path.join(__dirname, 'less', 'includes') ]
    }).on('error', function (e){
            console.error(e.message);
            this.emit('end');
        })
    )
    .pipe( cleanCSS() )
    // .pipe(gulp.dest('resource/css'))
    // .pipe(rev())
    .pipe(gulp.dest('production/style'))
    // .pipe(rev.manifest())                 
    // .pipe(gulp.dest('./rev'));
});


var libScripts = [
   'src/lib/jquery.min.js',
   'src/lib/angular.min.js',
   'src/lib/angular-ui-router.min.js',
   'src/lib/angular-cookies.min.js',
   'src/lib/angular-animate.min.js',
   'src/lib/angular-translate.min.js',
   'src/lib/ngStorage.min.js',
   'src/lib/ui-bootstrap-tpls.min.js',
   'src/lib/ui-load.js',
   'src/lib/ui-jq.js',
   'src/lib/ui-validate.js',
   'src/lib/angular-sanitize.min.js'
];
gulp.task('build:lib', function(){
  return gulp.src( libScripts )
      .pipe(uglify())
      .pipe(concat('bundle.min.js'))
      // .pipe(rev())
      .pipe(gulp.dest('production/lib'))
      // .pipe(rev.manifest())                 
      // .pipe(gulp.dest('./rev'));
});
gulp.task('build:depslib', function(){
  gulp.src( 'src/deps-lib/**/*.js' )
      .pipe(uglify())
      .pipe(gulp.dest('production/deps-lib'))
  gulp.src( 'src/deps-lib/**/*.css' )
      .pipe( cleanCSS() )
      .pipe(gulp.dest('production/deps-lib'))
  gulp.src( 'src/deps-lib/**/*.swf' )
      .pipe(gulp.dest('production/deps-lib'))
});




gulp.task('build:index', function(){
  gulp.src('src/crossdomain.xml')
    .pipe(gulp.dest('production/'));
    
    return gulp.src('src/index.html')
      .pipe(htmlreplace({
          'css': 'style/bundle.min.css',
          'lib': 'lib/bundle.min.js'
      }))
      .pipe(rename('index.ejs'))
      .pipe(gulp.dest('views'));
});



// gulp.task('rev', function() {
//   //- 读取 rev-manifest.json 文件以及需要进行css名替换的文件
//     gulp.src(['./rev/*.json', './src/*.html'])                    
//   //- 执行文件内css名的替换
//     .pipe(revCollector())                                   
//   //- 替换后的文件输出的目录
//     .pipe(gulp.dest('views/'));                                
    
// });


gulp.task( 'build', [ 'build:images', 'build:index','build:lib','build:less', 'build:javascripts', 'build:page:html', 'fonts', 'build:depslib']);

gulp.task('default', ['build'], function() {
    console.log( 'production build successfully !');
});
gulp.start('default');







